package com.example.demo.service.impl;

import com.example.demo.entity.ImageClass;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.ErrorCode;
import com.example.demo.mapper.ImageClassMapper;
import com.example.demo.service.ImageClassService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImageClassServiceImpl implements ImageClassService {

    private static final Logger logger = LoggerFactory.getLogger(ImageClassServiceImpl.class);

    @Autowired
    private ImageClassMapper imageClassMapper;


    @Override
    public ImageClass createImageClass(ImageClass imageClass) {
        // 检查分类名称是否已存在
        ImageClass existing = imageClassMapper.selectImageClassByName(imageClass.getName());
        if (existing != null) {
            logger.warn("Image class already exists: {}", imageClass.getName());
            throw new BusinessException(ErrorCode.IMAGE_CLASS_ALREADY_EXISTS);
        }

        // 设置创建时间和更新时间
        Date now = new Date();
        imageClass.setCreate_time(now);
        imageClass.setUpdate_time(now);

        // 插入数据，ID会自动填充到imageClass对象中
        imageClassMapper.insertImageClass(imageClass);
        logger.info("Created new image class: {}", imageClass.getName());
        return imageClass;
    }

    @Override
    public boolean updateImageClass(ImageClass imageClass) {
        // 检查分类是否存在
        ImageClass existing = imageClassMapper.selectImageClassById(imageClass.getId());
        if (existing == null) {
            logger.warn("Image class not found: {}", imageClass.getId());
            throw new BusinessException(ErrorCode.IMAGE_CLASS_NOT_FOUND);
        }

        // 设置更新时间
        imageClass.setUpdate_time(new Date());

        // 更新数据
        imageClassMapper.updateImageClass(imageClass);
        logger.info("Updated image class: {}", imageClass.getId());
        return true;
    }

    @Override
    public boolean deleteImageClass(Integer id) {
        // 检查分类是否存在
        ImageClass existing = imageClassMapper.selectImageClassById(id);
        if (existing == null) {
            logger.warn("Image class not found: {}", id);
            throw new BusinessException(ErrorCode.IMAGE_CLASS_NOT_FOUND);
        }

        // 删除数据
        imageClassMapper.deleteImageClass(id);
        logger.info("Deleted image class: {}", id);
        return true;
    }

    @Override
    public Map<String, Object> getImageClassList(Integer page, Integer limit, String keyword) {
        Map<String, Object> params = new HashMap<>();
        params.put("keyword", keyword);

        // 使用PageHelper分页
        PageHelper.startPage(page, limit);
        List<Map<String, Object>> list = imageClassMapper.selectImageClassList(params);
        
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("totalCount", pageInfo.getTotal());
        
        logger.debug("Fetched image class list, page: {}, limit: {}", page, limit);
        return data;
    }

    @Override
    public ImageClass selectImageClassById(Integer id) {
        ImageClass imageClass = imageClassMapper.selectImageClassById(id);
        if (imageClass == null) {
            logger.warn("Image class not found: {}", id);
            throw new BusinessException(ErrorCode.IMAGE_CLASS_NOT_FOUND);
        }
        return imageClass;
    }

    @Override
    public Map<String, Object> getImageListByClass(Integer id, Integer page, Integer limit) {
        // 检查分类是否存在
        ImageClass imageClass = imageClassMapper.selectImageClassById(id);
        if (imageClass == null) {
            logger.warn("Image class not found: {}", id);
            throw new BusinessException(ErrorCode.IMAGE_CLASS_NOT_FOUND);
        }

        // 使用PageHelper分页
        PageHelper.startPage(page, limit);
        // String url = RequestUtils.getServerUrl();
        List<Map<String, Object>> list = imageClassMapper.selectImageListByClassId(id, "");
        
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = new HashMap<>();
        data.put("list", pageInfo.getList());
        data.put("totalCount", pageInfo.getTotal());
        
        logger.debug("Fetched image list by class: {}, page: {}, limit: {}", id, page, limit);
        return data;
    }
}
